package com.yc.qunxing.mapper;

import com.yc.qunxing.entity.Drivers;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 鹏哥
 * @since 2024-05-23
 */
public interface DriversMapper extends BaseMapper<Drivers> {

    @Insert("INSERT INTO drivers(username, password, phone_number, license_plate, status, email) " +
            "VALUES(#{username}, #{password}, #{phoneNumber}, #{licensePlate}, #{status}, #{email})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertAndGetId(Drivers drivers);

    @Select("SELECT d.id, d.username, d.phone_number, d.license_plate, " +
            "dv.vehicle_type AS vehicleType, dv.color AS color " +
            "FROM drivers d " +
            "LEFT JOIN driver_vehicles dv ON d.id = dv.driver_id " +
            "WHERE d.id = #{driverId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "phoneNumber", column = "phone_number"),
            @Result(property = "licensePlate", column = "license_plate"),
            @Result(property = "driverVehicles.vehicleType", column = "vehicleType"),
            @Result(property = "driverVehicles.color", column = "color")
    })
    Drivers getDriverDetailsById(Integer driverId);
    @Select("SELECT d.*, dv.vehicle_type AS vehicleType, dv.color AS color FROM drivers d " +
            "LEFT JOIN driver_vehicles dv ON d.id = dv.driver_id " +
            "WHERE (#{keyword} IS NULL OR d.id LIKE CONCAT('%', #{keyword}, '%')) " +
            "AND (#{keywordOrder} IS NULL OR d.status LIKE CONCAT('%', #{keywordOrder}, '%')) " +
            "AND (#{searchVehicle} IS NULL OR dv.vehicle_type LIKE CONCAT('%', #{searchVehicle}, '%') " +
            "OR dv.color LIKE CONCAT('%', #{searchVehicle}, '%'))")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "phoneNumber", column = "phone_number"),
            @Result(property = "licensePlate", column = "license_plate"),
            @Result(property = "driverVehicles.vehicleType", column = "vehicleType"),
            @Result(property = "driverVehicles.color", column = "color")
    })
    List<Drivers> getDriverByManager(@Param("keyword") String keyword,
                                     @Param("keywordOrder") String keywordOrder,
                                     @Param("searchVehicle") String searchVehicle);
}
